using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectLienKet()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LIENKET
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectLienKetPaging(int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LIENKET,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListLienKet", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteLienKet(int id)
		{
			return Json(new{
				Success = _repLienKet.Delete(id),
				RecordCount = _repLienKet.SelectAll().Count,
				DeleteId = id
			});
		}

		public ActionResult EditLienKet(int page, int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LIENKET,
				CurrentPage = page,
				EditID = id
			};
			return PartialView("Templates/TH_EditLienKet", CreateViewModel(data));
		}

		public ActionResult CancelEditingLienKet(int page)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LIENKET,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListLienKet", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateLienKet(FormCollection forms)
		{
			string id = forms.GetValues("LienKet_ID")[0];
			string tenlienket = forms.GetValues("LienKet_TenLienket")[0];
			string url = forms.GetValues("LienKet_Url")[0];
			string currentpage = forms.GetValues("LienKet_CurrentPage")[0];
			var lienket = _repLienKet.SelectByID(int.Parse(id));
			lienket.TenLienket = tenlienket;
			lienket.Url = url;
			var result = _repLienKet.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LIENKET,
				CurrentPage = int.Parse(currentpage)
			};
			return PartialView("Templates/TH_ListLienKet", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertLienKet(FormCollection forms)
		{
			string tenlienket = forms.GetValues("LienKet_TenLienket")[0];
			string url = forms.GetValues("LienKet_Url")[0];
			var newItem = new LienKet
			{
				TenLienket = tenlienket,
				Url = url
			};
			var result = _repLienKet.Insert(newItem);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LIENKET,
				Added = result
			};
			return Json(new{
				Success = result,
				Message = "A new item has been added!",
				PartialViewHtml = RenderPartialViewToString("Templates/TH_ListLienKet", CreateViewModel(data))
			});
		}


	}
}